<template>
    <div>
        <Input placeholder="请选择组织机构" v-model="orgName" @on-focus="openOrgModal" suffix="ios-search"/>
        <Modal v-model="modal" width="900" :mask-closable="false" title="组织机构列表" footer-hide>
            <Form :label-width="110" ref="formInline" :model="formInline">
                <Row :gutter="16" class="query-form">
                    <i-col span="8">
                        <FormItem label="公司名称：">
                            <Input class="query-form-el" v-model="formInline.orgName"></Input>
                        </FormItem>
                    </i-col>
                    <i-col span="8">
                        <FormItem label="组织机构级别：">
                            <Select class="query-form-el" filterable v-model="formInline.orgLevel" >
                                <Option v-for="item in orgLevelList" :value="item.codingCodeId" :key="item.codingCodeId">{{ item.codingName }}</Option>
                            </Select>
                        </FormItem>
                    </i-col>
                    <i-col span="8">
                        <div style="float: right;">
                            <Button class="ivu-btn-reset" @click="resetOrg">重置</Button>
                            <Button class="ivu-btn-query" icon="md-search"  @click="queryOrg">查询</Button>
                        </div>
                    </i-col>
                </Row>
            </Form>
            <Table row-key='orgId' ref='table2' height="400" :loading="loading2" no-data-text="没有查询到相关数据!" border @on-select="selectionTable"
                   :columns="orgColumn" :load-data="handleLoadData" width="100%" :data="orgDatas" size="small">
            </Table>
        </Modal>
    </div>
</template>
<script>
    import { sessionStorage } from '../common/cacheUtils.js';
    import {Api} from '../api/index.js'
    export default {
        props: {
            orgInfo: {},
        },
        data() {
            return{
                modal: false,
                orgDatas: [],
                formInline: {},
                loading2: false,
                orgLevelList: [],
                orgColumn:[
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center',
                    },
                    {
                        title: '组织机构名称',
                        minWidth: 200,
                        align: 'left',
                        key: 'orgName',
                        tree:true,
						render:(h, params) => {
						    return h('span', params.row.orgName+ ((params.row.orgType=='1'||params.row.orgLevel=='10'||params.row.orgLevel=='0200100005000000001')?'':('('+params.row.company_name+')')));
						}
                    },
                    {
                        title: '组织机构级别',
                        width: 150,
                        align: 'center',
                        key: 'orgLevel',
                        render:(h, params) => {
                            return h('span', this.orgLevelMap[params.row.orgLevel]);
                        }
                    },
                ],
                orgName: '',
                orgLevelMap:{}
            }
        },
        mounted(){
            this.selectCodingSort();
        },
        methods: {
            selectCodingSort(){
                this.$http.get(Api.queryCodeDetailBySortId +'/0200100005',{params:{}}).then((res) => {
                    this.orgLevelList = res.data.data;
                    this.orgLevelList.forEach(item=>{
                        this.orgLevelMap[item.codingCodeId] = item.codingName;
                    })
                });
            },
            setOrgName(orgName){
                this.orgName = orgName;
            },
            openOrgModal(){
                this.modal = true;
                this.getOrgInfo();
            },
            getOrgInfo(){
                this.$http.get(Api.queryOrgDetailById + "/" + sessionStorage('userId').getObject('userId').companyId).then(res => {
                    this.orgDatas = [];
                    let temp = {};
                    temp._loading = false;
                    temp.children = [];
                    temp.orgName = res.data.data.orgName;
                    temp.orgId = res.data.data.orgId;
                    temp.orgLevel = res.data.data.orgLevel;
                    temp.orgType = res.data.data.orgType;
                    this.orgDatas.push(temp);
                }).catch(e => {
                    this.$Message.error('请求失败');
                })
            },
            handleLoadData (item, callback) {
                setTimeout(() => {
                    this.$http.get(Api.queryOrg, {
                        params: {
                            parentId: item.orgId,
                            codeAffordIf : 0
                        }
                    }).then(res => {
                        if(res.data.data.length !== 0) {
                            res.data.data.map(i => {
                                if(i.orgLevel != '0200100005000000007'){
                                    i._loading = false;
                                    i.children = [];
                                }
                            })
                            item.children = res.data.data;
                            callback(item.children);
                        } else {
                            delete item['_loading'];
                            item.children=[];
                            callback([]);
                        }
                    }).catch(e => {
                        this.$Message.error('请求失败');
                    })
                }, 2000);
            },
            selectionTable(selection, row){
                this.modal = false;
                this.orgName = row.orgName;
                this.$emit('setOrgInfo', row);
            },
            resetOrg(){
                this.formInline.orgName = '';
                this.formInline.orgLevel = '';
                this.getOrgInfo();
            },
            queryOrg(){
                this.loading2 = true;
                this.$http.get(Api.queryOrgList+"?orgName="+this.formInline.orgName+"&fromPage=1&pageSize=1000&companyId="+sessionStorage('userId').getObject('userId').companyId).then(res => {
                    let datas = [];
                    res.data.data.map(i => {
                        if(i.orgLevel == '13' || i.orgLevel == '12'){
                            return;
                        }
                        if(i.orgLevel != '0200100005000000007'){
                            i._loading = false;
                            i.children = [];
                        }
                        datas.push(i);
                    })
                    this.orgDatas = datas;
                    this.loading2 = false;
                }).catch(e => {
                    this.$Message.error('请求失败');
                    this.loading2 = false;
                })
            },
            resetCurrentOrg(){
                this.formInline.orgName = '';
                this.formInline.orgLevel = '';
                this.orgName = '';
            },
            setCurrentOrg(){
                this.orgName = this.orgInfo.name;
            },
            cancel(){
                this.formInline= {};
                this.queryOrg();
            }
        }
    }
</script>

<style lang='css' scoped>
    @import '../index.css';

</style>
